/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/* 
 * Copyright (c) 2009 City University of Hong Kong
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License m_version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Author: Wilson Thong (wilsonwk@ee.cityu.edu.hk) 
 */
#ifndef CONTAINER_HELPER
#define CONTAINER_HELPER

#include "jitter-sim-include.h"
#include "ns3/callback.h"
#include "ns3/ptr.h"

namespace ns3 {
namespace eecityu {

using namespace std;

/**
 * \brief Help doing calculations or execution on a collection of objects
 */
class ContainerHelper
{
public:
  /**
   * \brief Run the specified member method on all the objects iterated
   *
   * \param start_it The starting iterator of the collection of objects
   * \param end_it The ending iterator of the collection of objects
   * \param mem_ptr The pointer pointing to the method that all 
   * objects in the collection are going to be run
   */
  template <typename R, typename C, typename CIterator> static void
    RunAll (CIterator start_it,
            CIterator end_it, 
            R (C::*mem_ptr) ());

  /**
   * \brief Run the specified member method on all the objects iterated 
   *
   * \param start_it The starting iterator of the collection of objects
   * \param end_it The ending iterator of the collection of objects
   * \param mem_ptr The pointer pointing to the method that all 
   * \param t1 The argument passing to the method being called
   * objects in the collection are going to be run
   *
   * Note that the type _T1 and T1 should be compatiable in order to
   * pass the compilation successfully
   */
  template <typename R, typename C, typename _T1, typename T1, typename CIterator> static void
    RunAll (CIterator start_it,
            CIterator end_it, 
            R (C::*mem_ptr) (_T1), 
            T1 t1);
};

template <typename R, typename C, typename CIterator> void
ContainerHelper::RunAll 
  (CIterator start_it,
   CIterator end_it,  
   R (C::*mem_ptr) ())
{
  // walk through all objects and run their 'mem_ptr' method 
  for (CIterator it = start_it; it != end_it; it++)
  {
    Callback<R> cb;
    cb.Assign (MakeCallback (mem_ptr, PeekPointer (*it)));
    cb ();
  }
}

template <typename R, typename C, typename _T1, typename T1, typename CIterator> void
ContainerHelper::RunAll 
  (CIterator start_it,
   CIterator end_it, 
   R (C::*mem_ptr) (_T1), 
   T1 t1)
{
  // walk through all objects and run their 'mem_ptr' method 
  for (CIterator it = start_it; it != end_it; it++)
  {
    Callback<R,_T1> cb;
    cb.Assign (MakeCallback (mem_ptr, PeekPointer (*it)));
    cb (t1);
  }
}

} // namespace eecityu
} // namespace ns3

#endif // #ifndef CONTAINER_HELPER
